﻿Imports System.Windows.Forms

Public Class DatagridViewEdComboBoxEditingControl
    Inherits StylishComboBox
    Implements IDataGridViewEditingControl

    Protected RowIndex As Integer
    Protected iDataGridView As DataGridView
    Protected valueChanged As Boolean = False

    'Notify DataGridView that the value has changed.
    Protected Sub NotifyDataGridViewOfValueChange()
        Me.valueChanged = True
        If Me.iDataGridView IsNot Nothing Then
            Me.iDataGridView.NotifyCurrentCellDirty(True)
        End If
    End Sub

    Public Sub ApplyCellStyleToEditingControl(dataGridViewCellStyle As System.Windows.Forms.DataGridViewCellStyle) Implements System.Windows.Forms.IDataGridViewEditingControl.ApplyCellStyleToEditingControl
        Me.Font = dataGridViewCellStyle.Font
        Me.ForeColor = dataGridViewCellStyle.ForeColor
        Me.BackColor = dataGridViewCellStyle.BackColor
        'Me.TextAlign = translateAlignment(dataGridViewCellStyle.Alignment)
    End Sub

    Public Property EditingControlDataGridView As System.Windows.Forms.DataGridView Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlDataGridView
        Get
            Return Me.iDataGridView
        End Get
        Set(value As System.Windows.Forms.DataGridView)
            Me.iDataGridView = value
        End Set
    End Property

    Public Property EditingControlFormattedValue As Object Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlFormattedValue
        Get
            Return Me.Text
        End Get
        Set(value As Object)
            Me.Text = value
            NotifyDataGridViewOfValueChange()
        End Set
    End Property

    Public Property EditingControlRowIndex As Integer Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlRowIndex
        Get
            Return Me.RowIndex
        End Get
        Set(value As Integer)
            Me.RowIndex = value
        End Set
    End Property

    Public Property EditingControlValueChanged As Boolean Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlValueChanged
        Get
            Return Me.valueChanged
        End Get
        Set(value As Boolean)
            Me.valueChanged = value
        End Set
    End Property

    Public Function EditingControlWantsInputKey(keyData As System.Windows.Forms.Keys, dataGridViewWantsInputKey As Boolean) As Boolean Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlWantsInputKey
        Select Case (keyData And Keys.KeyCode)
            Case Is = Keys.Right
                If (Not (Me.SelectionLength = 0 AndAlso Me.SelectionStart = Me.ToString().Length)) Then
                    Return True
                End If

            Case Is = Keys.Left
                If (Not (Me.SelectionLength = 0 AndAlso Me.SelectionStart = 0)) Then
                    Return True
                End If
            Case Is = Keys.End
                If (Me.SelectionLength <> Me.ToString().Length) Then
                    Return True
                End If
            Case Is = Keys.Next
                If Me.valueChanged Then
                    Return True
                End If
            Case Is = Keys.Delete
                If (Me.SelectionLength > 0 OrElse Me.SelectionStart < Me.ToString().Length) Then
                    Return True
                End If
        End Select
        'defer to the DataGridView and see if it wants it.
        Return Not dataGridViewWantsInputKey
    End Function

    Public ReadOnly Property EditingPanelCursor As System.Windows.Forms.Cursor Implements System.Windows.Forms.IDataGridViewEditingControl.EditingPanelCursor
        Get
            Return Cursors.IBeam
        End Get
    End Property

    Public Function GetEditingControlFormattedValue(context As System.Windows.Forms.DataGridViewDataErrorContexts) As Object Implements System.Windows.Forms.IDataGridViewEditingControl.GetEditingControlFormattedValue
        Return Me.Text
    End Function

    Public Sub PrepareEditingControlForEdit(selectAll As Boolean) Implements System.Windows.Forms.IDataGridViewEditingControl.PrepareEditingControlForEdit
        If selectAll Then
            Me.SelectAll()
        Else
            Me.SelectionStart = Me.Text.Length
        End If
    End Sub

    Public ReadOnly Property RepositionEditingControlOnValueChange As Boolean Implements System.Windows.Forms.IDataGridViewEditingControl.RepositionEditingControlOnValueChange
        Get
            Return False
        End Get
    End Property

    Protected Overrides Sub OnSelectedIndexChanged(e As System.EventArgs)
        MyBase.OnSelectedIndexChanged(e)
        Me.Text = Me.Items(Me.SelectedIndex)
        Me.valueChanged = True
        Me.NotifyDataGridViewOfValueChange()
    End Sub

    Protected Overrides Sub OnTextChanged(e As System.EventArgs)
        MyBase.OnTextChanged(e)
        Me.valueChanged = True
        Me.NotifyDataGridViewOfValueChange()
    End Sub

End Class
